package com.itheima.test04;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/*
题目四:
	请按以下步骤编写程序：
定义一个线程类，实现Callable接口。此线程可以计算1--100的所有数字的累加和。
定义测试类，和main()方法，使用线程池启动线程，并获取计算结果，并将结果打印到控制台。
 */
public class Test {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        MyCallable myCallable;

        for (int i = 1; i < 101; i++) {
            myCallable = new MyCallable(i);
            FutureTask futureTask = new FutureTask<String>(myCallable);
            executorService.submit(new Thread(futureTask,"线程" + i));
            System.out.println(futureTask.get());
//            new Thread(futureTask,"线程" + i).start();
        }
        Thread.sleep(2000);

        executorService.shutdown();
    }
}
